<?php

namespace app\admin\controller;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Aprcard extends Backend
{
    
    /**
     * Cards模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Cards');

    }

    public function index()
    {
        //设置过滤方法
        //$this->request->filter(['strip_tags', 'htmlspecialchars']);
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)->where('type=2')->with('user')
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)->where('type=2')->with('user')
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach($list as $k => $v){
                if($v['stuff']==0){
                    $list[$k]['stuff'] ='系统发送';
                }else{
                    $admin = Db::name('admin')->field('nickname')->where('id',$v['stuff'])->find();
                    $list[$k]['stuff'] = $admin['nickname'];
                }
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function add()
    {
        if ($this->request->isPost())
        {
            $this->code = -1;
            $params = $this->request->post("row/a");

            $user = Db::name('user')->where('phone',$params['userId'])->find();
            if($user){
                $params['userId'] = $user['userId'];
            }else{
                $this->msg = __('此用户不存在，无法添加加息卡');
                return;
            }

            if ($params)
            {
                foreach ($params as $k => &$v)
                {
                    $v = is_array($v) ? implode(',', $v) : $v;
                }
                try
                {
                    //是否采用模型验证
                    if ($this->modelValidate)
                    {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                        $this->model->validate($validate);
                    }

                    $params['type'] = 2;
                    $params['createTime'] = time();
                    $params['stuff'] = $this->auth->id;
                    $params['isUse'] = 0;
                    $params['endTime'] = strtotime($params['endTime'])+24*3600-1;
                    $params['actType'] = 2;

                    $result = $this->model->save($params);
                    if ($result !== false)
                    {
                        $this->code = 1;
                    }
                    else
                    {
                        $this->msg = $this->model->getError();
                    }
                }
                catch (\think\exception\PDOException $e)
                {
                    $this->msg = $e->getMessage();
                }
            }
            else
            {
                $this->msg = __('Parameter %s can not be empty', '');
            }

            return;
        }
        return $this->view->fetch();
    }


    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));
        if ($this->request->isPost())
        {
            $this->code = -1;
            $params = $this->request->post("row/a");
            if ($params)
            {
                foreach ($params as $k => &$v)
                {
                    $v = is_array($v) ? implode(',', $v) : $v;
                }
                try
                {
                    //是否采用模型验证
                    if ($this->modelValidate)
                    {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    $params['endTime'] = strtotime($params['endTime'])+24*3600-1;
                    $result = $row->save($params);
                    if ($result !== false)
                    {
                        $this->code = 1;
                    }
                    else
                    {
                        $this->msg = $row->getError();
                    }
                }
                catch (think\exception\PDOException $e)
                {
                    $this->msg = $e->getMessage();
                }
            }
            else
            {
                $this->msg = __('Parameter %s can not be empty', '');
            }

            return;
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
    

}
